package com.shenjialin.Tank;

import com.shenjialin.Game.Bullet;
import com.shenjialin.Game.Explode;
import com.shenjialin.Game.GameFrame;
import com.shenjialin.util.BulletsPool;
import com.shenjialin.util.Constant;
import com.shenjialin.util.MyUtil;
import com.shenjialin.util.TankPool;

import java.util.*;
import java.awt.*;
import java.util.List;


public class EnemyTank extends Tank{
    protected static Image[] tankImg;

    // 记录 5 秒开始的时间
    private long aiTime;
    static {
        tankImg = new Image[4];
        tankImg[0] = MyUtil.createImage("res/tankelseup.png");
        tankImg[1] = MyUtil.createImage("res/tankelsedown.png");
        tankImg[2] = MyUtil.createImage("res/tankelseleft.png");
        tankImg[3] = MyUtil.createImage("res/tankelseright.png");

    }

    public EnemyTank() {
        color = MyUtil.getRandomColor();
        name = MyUtil.getName();
    }

    private void drawImgTank(Graphics g) {
        ai();
        g.drawImage(tankImg[dir], x - RADIUS, y - RADIUS, null);
    }
    public void draw(Graphics g) {

        logic();
//        drawTank(g);
        drawImgTank(g);

        drawBullets(g);

        drawName(g);

        bar.draw(g);
    }

    public EnemyTank(int x, int y, int dir) {

        super(x, y, dir);
        //敌人一旦创建 就可以进行即使
        aiTime = System.currentTimeMillis();
    }

    public static EnemyTank createEnemy() {
        int x = MyUtil.getRandomNumber(0,2) == 0 ? RADIUS : Constant.FRAME_WIDTH - RADIUS;
        int y = Constant.FRAME_HEIGHT - RADIUS;
        int dir = DIR_UP;
//        EnemyTank enemyTank = new EnemyTank(x,y,dir);

        EnemyTank enemyTank = TankPool.get();
        enemyTank.aiTime = System.currentTimeMillis();
        enemyTank.setX(x);
        enemyTank.setY(y);
        enemyTank.setDir(dir);
        enemyTank.isEnemy = true;
        enemyTank.setSpeed(Tank.DEFAULT_SPEED/3);
        enemyTank.setState(STATE_MOVE);
        enemyTank.setHp(Tank.DEFAULT_HP);
        //TODO
        return enemyTank;
    }

    private void ai() {
        if(System.currentTimeMillis() - aiTime > Constant.ENEMY_AI_INTERVAL) {
            // 间隔 5 秒 随机一个状态
            setDir(MyUtil.getRandomNumber(DIR_UP,DIR_RIGHT + 1));
            setState(MyUtil.getRandomNumber(0,2) == 0 ? STATE_MOVE : STATE_STAND);
            aiTime = System.currentTimeMillis();
        }
        if(Math.random() < Constant.ENEMY_AI_FIRE_PERCENT){
            fire();
        }
    }


    protected void die() {
        // 敌人坦克被消灭 归还对象池
        TankPool.theReturn(this);
    }
    @Override
    public void fire() {
        int bulletX = x;
        int bulletY = y;
        switch (dir) {
            case DIR_UP:
                bulletY -= RADIUS;
                break;
            case DIR_DOWN:
                bulletY += RADIUS;
                break;
            case DIR_LEFT:
                bulletX -= RADIUS;
                break;
            case DIR_RIGHT:
                bulletX += RADIUS;
                break;
        }
        //
        Bullet bullet = BulletsPool.get();
        bullet.setBulletType(Bullet.BULLET_TYPE0);
        bullet.setX(bulletX);
        bullet.setY(bulletY);
        bullet.setDir(dir);
        bullet.setSpeed(speed * 2);
        bullet.setAtk(atk);
        bullet.setColor(color);
        bullet.setVisible(true);
        //= new Bullet(bulletX,bulletY,dir,atk,color);
        bullets.add(bullet);
    }
}
